import { createParamDecorator, SetMetadata } from '@nestjs/common';
import { ExecutionContext } from '@nestjs/common';

export const RequireLogin = () => SetMetadata('require-login', true);

// 负责解析从 JWT 取出放到 request.user 中的用户信息
export const UserInfo = createParamDecorator(
  (key: string, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();

    if (!request.user) {
      return null;
    }

    return key ? request.user[key] : request.user;
  },
);
